package com.xuecheng.manage_cms.web.controller;

import com.xuecheng.framework.web.BaseController;
import com.xuecheng.manage_cms.service.PageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.ServletOutputStream;
import java.io.IOException;

/**
 * @author: gongfy
 * @create: 2020/7/12 16:14
 * @Params:  pageid 页面id
 * @Description: 页面预览controller 继承BaseController 获取request response
 */
@Controller
public class CmsPagePreviewController extends BaseController {
    @Autowired
    private PageService pageService;
    /**
    * @Author: gongfy
    * @Param:  页面id
    * @Return:
    * @Date: 2020/8/4 20:25
    * @Description: 页面预览
              // 页面预览 前端
             preview:function (pageId) { window.open("http://www.xuecheng.com/cms/preview/"+pageId);},
    **/
    @GetMapping(value = "/cms/preview/{pageid}")
    public void pagePreview(@PathVariable(value = "pageid") String pageid ){
        String pageHtml = "";
        try {
            pageHtml = pageService.getPageHtml(pageid);
            if (StringUtils.isNotEmpty(pageHtml)){
                ServletOutputStream outputStream = response.getOutputStream();
                //由于 Nginx先请求cms的课程预览功能得到html页面，再解析页面中的ssi标签，
                //这里必须保证cms页面预览返回的页面的Content-Type为text/html;charset=utf-8
                response.setHeader("Content-type","text/html;charset=utf-8"); //  todo ‐ -太坑了 还不报错
                //将静态化页面内容通过浏览器响应
                outputStream.write(pageHtml.getBytes("utf-8"));

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
